/**
 * 
 */
package ru.cargobroker.maip.base.util;

import java.text.MessageFormat;
import java.util.ResourceBundle;

/**
 * @author M.Frendiy
 * @since 0.0.1
 */
public class ResourceBundleUtils {

	// TODO: make as bean
	private static Localization localization = LocalizationFactory.createLocalization();
	
	public static String getValue(ResourceBundle bundle, String key,
			String defaultValue) {
		try {
			if (bundle != null) {
				return bundle.getString(key);
			} else {
				return defaultValue;
			}
		} catch (Exception e) {
			return defaultValue;
		}
	}

//	public static String getStringValue(String resourcename, Locale locale,
//			String key, String defaultValue) {
//		try {
//			ResourceBundle myBundle = ResourceBundle.getBundle(resourcename,
//					locale);
//			return getStringValue(myBundle, key, defaultValue);
//		} catch (Exception e) {
//			return defaultValue;
//		}
//	}
	
	public static String getValue(String resourcename,
			String key, String defaultValue) {
		try {
			ResourceBundle myBundle = ResourceBundle.getBundle(resourcename,
					getLocalization().getLocale());
			return getValue(myBundle, key, defaultValue);
		} catch (Exception e) {
			return defaultValue;
		}
	}

	public static String getValue(ResourceBundle bundle, String key,
			String defaultValue, Object... args) {
		 return MessageFormat.format(getValue(bundle,key,defaultValue), args);
	}

//	public static String getValue(String resourcename, Locale locale,
//			String key, String defaultValue, Object... args) {
//		try {
//			ResourceBundle myBundle = ResourceBundle.getBundle(resourcename,
//					locale);
//			return getValue(myBundle, key, defaultValue, args);
//		} catch (Exception e) {
//			return defaultValue;
//		}
//	}
	
	public static String getValue(String resourcename,
			String key, String defaultValue, Object... args) {
		try {
			ResourceBundle myBundle = ResourceBundle.getBundle(resourcename,
					getLocalization().getLocale());
			return getValue(myBundle, key, defaultValue, args);
		} catch (Exception e) {
			return defaultValue;
		}
	}
	
    public static Localization getLocalization() {
        return  localization;
    }

	public static final String DEFAULT_MARKER = "DEFAULT_MARKER";

}
